package cn.hn3l.agriculture.controllers;

import cn.hn3l.agriculture.entitis.behoof.Crop;
import cn.hn3l.agriculture.entitis.behoof.Sympton;
import cn.hn3l.agriculture.entitis.goods.Goods;
import cn.hn3l.agriculture.entitis.goods.GoodsType;
import cn.hn3l.agriculture.entitis.goods.Productor;
import cn.hn3l.agriculture.services.interfaces.datum.EffectService;
import cn.hn3l.agriculture.services.interfaces.goods.GoodsService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Wang Weiwei
 * @Since 16-10-19
 * @Describe
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    private Logger logger = LoggerFactory.getLogger(GoodsController.class);
    @Resource
    private GoodsService goodsService;

    @Resource
    private EffectService effectService;
    /**查看商品详情信息*/
    @RequestMapping("/detail_goods/{id}")
    public String detailGoods(@PathVariable int id, Model model){
        Goods goods = goodsService.findGoodsById(id);
        model.addAttribute("goods",goods);
        return "goods/detail_goods_content";
    }

    /**查询所有商品相关属性*/
    @ResponseBody
    @RequestMapping(value = "/goodsAtribute/all",
                    produces = "application/json;charset=UTF-8")
    public String getGoodsAttribute(){
        List<GoodsType> goodsTypeList = goodsService.findAllGoodsType();
        List<Crop> cropList = effectService.findAllCrop();
        List<Sympton> symptonList = effectService.findAllSympton();
        Map<String,List> stringListMap = new HashMap<>();
        stringListMap.put("goodsTypeList",goodsTypeList);
        stringListMap.put("cropList",cropList);
        stringListMap.put("symptonList",symptonList);
        return JSON.toJSONString(stringListMap);
    }

    @ResponseBody
    @RequestMapping(value = "/productors/search",
            produces = "application/json;charset=UTF-8",
            method = RequestMethod.GET)
    public String searchProductors(String param){
        List<String> productorList = goodsService.searchProductorsByName(param.split(" "));
        System.out.println(param);
        return goodsService.toSercherProviderJSONString(productorList);
    }

    @ResponseBody
    @RequestMapping("/test")
    public String testInterceptor(){
        logger.error("testInterceptor...执行了");
        return "testInterceptor...";
    }
}
